(function() {
    $.fn.extend = function(options) {
        var $this = $(this);
        var defaults = {
            speed: 1000,
            auto: true,
            times: 1000,
            mode: 1
        };
        var options = $.extend(defaults, options);
        var flag = false;
        var imgWidth = $this.find("ul li img").width();
        var imgHeight = $this.find("ul li img").height();
        $this.find("ul li").css({
            "height": imgHeight + "px"
        });
        var leftBtn = '<span class="left"></span>';
        var rightBtn = '<span class="right"></span>';
        var picNum = $this.find("ul li").length;
        $this.css({
            "width": imgWidth + "px",
            "height": imgHeight + "px"
        });
        if (options.mode == 1) {
            $this.children("ul").css({
                "width": (picNum + 1) * imgWidth + "px",
                "height": imgHeight + "px"
            });
            $this.find("ul li").addClass("aaa")
        } else {
            if (options.mode == 2) {
                $this.children("ul").css({
                    "width": imgWidth + "px",
                    "height": imgHeight + "px"
                });
                $this.find("ul li").addClass("ccc");
                $this.find("ul li:eq(0)").addClass("current");
            } else {
                if (options.mode == 3) {
                    $this.children("ul").css({
                        "width": imgWidth + "px",
                        "height": imgHeight + "px"
                    });
                    $this.find("ul li").addClass("bbb");
                    $this.find("ul li:eq(0)").show().siblings().hide();
                }
            }
        }
        var Carousel = "<ol></ol>";
        $this.append(leftBtn, rightBtn, Carousel);
        for (var i = 0; i < picNum; i++) {
            var html = '<li class="dotBtn"></li>';
            $this.children("ol").append(html)
        }
        var olWidth = $this.children("ol").width();
        $this.children("ol").css("margin-left", -olWidth / 2 + "px");
        $this.find("ol li").eq(0).addClass("current");
        if (options.mode == 1) {
            $this.children("ul").append($this.find("ul li").eq(0).clone(true))
        }
        var numcurrent = "carNum" + options.mode;
        numcurrent = 0;
        function rightNum() {
            numcurrent++;
            if (numcurrent > picNum - 1) {
                numcurrent = 0
            }
        }
        function leftNum() {
            numcurrent--;
            if (numcurrent < 0) {
                numcurrent = picNum - 1
            }
        }
        function nextFn() {
            if (flag) {
                return
            }
            numcurrent++;
            $this.children("ul").stop().animate({
                left: -numcurrent * imgWidth + "px"
            },
            options.speed,
            function() {
                flag = false;
                if (numcurrent > picNum - 1) {
                    numcurrent = 0;
                    $this.children("ul").css("left", "0")
                }
                $this.find("ol li").eq(numcurrent).addClass("current").siblings().removeClass("current")
            });
            flag = true
        }
        function prevFn() {
            if (flag) {
                return
            }
            numcurrent--;
            if (numcurrent < 0) {
                $this.children("ul").css("left", -picNum * imgWidth + "px");
                numcurrent = picNum - 1
            }
            $this.children("ul").stop().animate({
                left: -numcurrent * imgWidth + "px"
            },
            options.speed,
            function() {
                flag = false;
                $this.find("ol li").eq(numcurrent).addClass("current").siblings().removeClass("current")
            });
            flag = true
        }
        function fadeNextFn(e) {
            if (flag) {
                return
            }
            rightNum();
            $this.find("ul li").eq(numcurrent).stop().fadeIn(options.speed).siblings().stop().fadeOut(options.speed,
            function() {
                flag = false
            });
            $this.find("ol li").eq(numcurrent).addClass("current").siblings().removeClass("current");
            flag = true
        }
        function fadePrevFn(e) {
            if (flag) {
                return
            }
            leftNum();
            $this.find("ul li").eq(numcurrent).stop().fadeIn(options.speed).siblings().stop().fadeOut(options.speed,
            function() {
                flag = false
            });
            $this.find("ol li").eq(numcurrent).addClass("current").siblings().removeClass("current");
            flag = true
        }
        function fangdaNextFn(e) {
            if (flag) {
                return
            }
            rightNum();
            $this.find("ul li").eq(numcurrent).addClass("current").siblings().removeClass("current");
            $this.find("ol li").eq(numcurrent).addClass("current").siblings().removeClass("current")
        }
        function fangdaPrevFn(e) {
            if (flag) {
                return
            }
            leftNum();
            $this.find("ul li").eq(numcurrent).addClass("current").siblings().removeClass("current");
            $this.find("ol li").eq(numcurrent).addClass("current").siblings().removeClass("current")
        }
        if (options.mode == 1) {
            $this.find(".right").bind("click", nextFn);
            $this.find(".left").bind("click", prevFn);
            $this.find(".dotBtn").bind("click",
            function() {
                $(this).addClass("current").siblings().removeClass("current");
                var dotIndex = $(this).index();
                $this.children("ul").stop().animate({
                    left: -dotIndex * imgWidth + "px"
                },
                options.speed);
                carNum = dotIndex
            })
        } else {
            if (options.mode == 2) {
                $this.find(".right").bind("click", fangdaNextFn);
                $this.find(".left").bind("click", fangdaPrevFn);
                $this.find(".dotBtn").bind("click",
                function() {
                    $(this).addClass("current").siblings().removeClass("current");
                    var dotIndex = $(this).index();
                    $this.find("ul li").eq(dotIndex).addClass("current").siblings().removeClass("current");
                    numcurrent = dotIndex
                })
            } else {
                if (options.mode == 3) {
                    $this.find(".right").bind("click", fadeNextFn);
                    $this.find(".left").bind("click", fadePrevFn);
                    $this.find(".dotBtn").bind("click",
                    function() {
                        $(this).addClass("current").siblings().removeClass("current");
                        var dotIndex = $(this).index();
                        $this.find("ul li").eq(dotIndex).stop().fadeIn(options.speed).siblings().stop().fadeOut(options.speed);
                        numcurrent = dotIndex
                    })
                }
            }
        }
        if (options.auto) {
            var timer = null;
            if (options.mode == 1) {
                timer1 = setInterval(nextFn, options.times)
            } else {
                if (options.mode == 2) {
                    timer2 = setInterval(fangdaNextFn, options.times)
                } else {
                    if (options.mode == 3) {
                        timer3 = setInterval(fadeNextFn, options.times)
                    }
                }
            }
            $this.hover(function() {
                if (options.mode == 1) {
                    clearInterval(timer1)
                } else {
                    if (options.mode == 2) {
                        clearInterval(timer2)
                    } else {
                        if (options.mode == 3) {
                            clearInterval(timer3)
                        }
                    }
                }
            },
            function() {
                if (options.mode == 1) {
                    timer1 = setInterval(nextFn, options.times)
                } else {
                    if (options.mode == 2) {
                        timer2 = setInterval(fangdaNextFn, options.times)
                    } else {
                        if (options.mode == 3) {
                            timer3 = setInterval(fadeNextFn, options.times)
                        }
                    }
                }
            })
        }
    }
})(jQuery);